package day52.system.service.impl;

import day52.system.domain.Menu;
import day52.system.mapper.MenuMapper;
import day52.system.service.IMenuService;
import day52.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Huang
 * @since 2022-12-26
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> queryTree() {
        //查询出所有的menu
        List<Menu> allMenus = menuMapper.findAll();
        HashMap<Long, Menu> hm = new HashMap<>();
        //结果集
        List<Menu> resultMenu = new ArrayList<>();;
        for (Menu allMenu : allMenus) {
            hm.put(allMenu.getId(),allMenu);
        }
        for (Menu menu : allMenus) {
            //找出所有的爹
            if(menu.getParentId() == null){
                resultMenu.add(menu);
            }else{
                //找出所有的儿子塞到爹的children里 menu在这里就是子部门了
                Menu parent = hm.get(menu.getParentId());
                //把儿子塞到爹的children里去
                parent.getChildren().add(menu);
            }
        }
        return resultMenu;
    }
}
